package xml;

import org.dom4j.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 *
 * @author liping(lpaidd50@gmail.com)
 * @version v1.0
 * @date 2016/8/8
 */
public class Main {
	public static void main(String[] args) throws DocumentException {
		String xml = "<xml><return_code><![CDATA[SUCCESS]]></return_code>\n"
						+ "<return_msg><![CDATA[OK]]></return_msg>\n"
						+ "<appid><![CDATA[wx4fc4b8ad832bd078]]></appid>\n"
						+ "<mch_id><![CDATA[1282861101]]></mch_id>\n"
						+ "<sub_mch_id><![CDATA[1363361102]]></sub_mch_id>\n"
						+ "<nonce_str><![CDATA[WFLoMczTWcZmzuYB]]></nonce_str>\n"
						+ "<sign><![CDATA[B4B2C16304DD72D35B2159C9DF3FA720]]></sign>\n"
						+ "<result_code><![CDATA[SUCCESS]]></result_code>\n"
						+ "<openid><![CDATA[oO33ys2S2G7yJEg16dhEoJD_mDmk]]></openid>\n"
						+ "<is_subscribe><![CDATA[Y]]></is_subscribe>\n"
						+ "<trade_type><![CDATA[NATIVE]]></trade_type>\n"
						+ "<bank_type><![CDATA[CFT]]></bank_type>\n"
						+ "<total_fee>500</total_fee>\n" + "<coupon_fee>176</coupon_fee>\n"
						+ "<fee_type><![CDATA[CNY]]></fee_type>\n"
						+ "<transaction_id><![CDATA[4006832001201608080855413168]]></transaction_id>\n"
						+ "<out_trade_no><![CDATA[537yjr2h4n8k8cggkwk0kog4g]]></out_trade_no>\n"
						+ "<attach><![CDATA[]]></attach>\n"
						+ "<time_end><![CDATA[20160808133215]]></time_end>\n"
						+ "<trade_state><![CDATA[SUCCESS]]></trade_state>\n"
						+ "<coupon_id_0><![CDATA[417712608]]></coupon_id_0>\n"
						+ "<coupon_id_1><![CDATA[691519826]]></coupon_id_1>\n"
						+ "<coupon_fee_0>88</coupon_fee_0>\n" + "<coupon_fee_1>88</coupon_fee_1>\n"
						+ "<coupon_count>2</coupon_count>\n"
						+ "<sub_appid><![CDATA[wx469d30c2a1fdcec8]]></sub_appid>\n"
						+ "<sub_openid><![CDATA[oe3KrwLu08ukgROEBvkaL8GTCcUc]]></sub_openid>\n"
						+ "<sub_is_subscribe><![CDATA[N]]></sub_is_subscribe>\n"
						+ "<cash_fee>324</cash_fee>\n" + "</xml>";
		
		Document document = DocumentHelper.parseText(xml);
		Element root = document.getRootElement();
		Map map = new HashMap<>();
		listNodes(root, map);
		System.out.println(map);
	}
	
	private static void listNodes(Element node, Map map) {
		if (!node.getName().equals("xml")) {
			map.put(node.getName(), node.getStringValue());
		}
		// 当前节点下面子节点迭代器
		Iterator<Element> it = node.elementIterator();
		// 遍历
		while (it.hasNext()) {
			// 获取某个子节点对象
			Element e = it.next();
			// 对子节点进行遍历
			listNodes(e, map);
		}
	}
}
